package com.aj.activity.adapter;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.aj.activity.R;
import com.aj.activity.view.CommentListView;
import com.aj.databases.entry.CmtInfo;
import com.aj.databases.entry.DigUserInfo;
import com.aj.databases.entry.PhotoInfo;
import com.aj.utils.Contants;
import com.aj.utils.PortraitLoaderUtil;

public class FamilyPhotoAdapter extends BaseAdapter {

	private Context context;

	private List<PhotoInfo> data;

	private HashMap<Integer, View> viewCache = new HashMap<Integer, View>();

	private IOperationInterface operationInterface;

	public interface IOperationInterface {
		public void shared(PhotoInfo photoInfo);

		public void report(PhotoInfo photoInfo);

		public void praise(PhotoInfo photoInfo, CompoundButton buttonView,
				boolean isChecked, TextView praise_text, ImageView imageView);

		public void commont(PhotoInfo photoInfo, CmtInfo cmtInfo);

		public void commontDelete(PhotoInfo photoInfo, CmtInfo cmtInfo);
	}

	public FamilyPhotoAdapter(Context context, List<PhotoInfo> data,
			IOperationInterface operationInterface) {
		this.context = context;
		this.data = data;
		this.operationInterface = operationInterface;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public class ViewHolder {
		TextView daynumber;
		TextView mouthnumber;
		Button datetime;
		ImageView photo;
		ImageView praise_image;
		Button share_to;
		Button report_to;
		CheckBox praise_to;
		Button comment_to;
		TextView praise_text;
		CommentListView comment_list;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		convertView = viewCache.get(position);
		final PhotoInfo photoInfo = data.get(position);
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_family_photo, null);
			viewHolder = new ViewHolder();
			viewHolder.daynumber = (TextView) convertView
					.findViewById(R.id.daynumber);
			viewHolder.mouthnumber = (TextView) convertView
					.findViewById(R.id.mouthnumber);
			viewHolder.datetime = (Button) convertView
					.findViewById(R.id.datetime);
			viewHolder.photo = (ImageView) convertView.findViewById(R.id.photo);
			viewHolder.praise_image = (ImageView) convertView
					.findViewById(R.id.praise_image);
			viewHolder.share_to = (Button) convertView
					.findViewById(R.id.share_to);
			viewHolder.report_to = (Button) convertView
					.findViewById(R.id.report_to);
			viewHolder.praise_to = (CheckBox) convertView
					.findViewById(R.id.praise_to);
			viewHolder.comment_to = (Button) convertView
					.findViewById(R.id.comment_to);
			viewHolder.praise_text = (TextView) convertView
					.findViewById(R.id.praise_text);
			viewHolder.comment_list = (CommentListView) convertView
					.findViewById(R.id.comment_list);
			convertView.setTag(viewHolder);
			PortraitLoaderUtil.loadSrc(context, viewHolder.photo, null,
					photoInfo.getPhoto(), R.drawable.nomal_photo,
					R.drawable.nomal_photo);
			viewCache.put(position, convertView);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.photo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(
						Contants.FAMILYPHOTOINFO_ACTIVITY_ACTION);
				intent.putExtra("albumList", (Serializable) data);
				intent.putExtra("index", position);
				context.startActivity(intent);

			}
		});

		List<DigUserInfo> digUserInfoList = photoInfo.getDigUserInfoList();
		if (digUserInfoList != null && digUserInfoList.size() > 0) {
			viewHolder.praise_text.setVisibility(View.VISIBLE);
			viewHolder.praise_image.setVisibility(View.VISIBLE);
			String digUserInfoString = "";
			for (DigUserInfo digUserInfo : digUserInfoList) {
				digUserInfoString += digUserInfo.getDig_user_nick() + ",";
			}
			digUserInfoString = digUserInfoString.substring(0,
					digUserInfoString.lastIndexOf(","));
			digUserInfoString += "觉得很赞";
			viewHolder.praise_text.setText(digUserInfoString);
		} else {
			viewHolder.praise_text.setVisibility(View.GONE);
			viewHolder.praise_image.setVisibility(View.GONE);
		}
		if (photoInfo.getHadDig() != null && photoInfo.getHadDig().equals("1")) {
			viewHolder.praise_to.setChecked(true);
		} else {
			viewHolder.praise_to.setChecked(false);
		}

		if (position == 0) {
			invisibilityDateInfo(viewHolder, View.VISIBLE, photoInfo);
		} else {
			if (photoInfo.getDate().equals(data.get(position - 1).getDate())) {
				invisibilityDateInfo(viewHolder, View.INVISIBLE, photoInfo);
			} else {
				invisibilityDateInfo(viewHolder, View.VISIBLE, photoInfo);
			}
		}
		CommontTextAdapter commontTextAdapter = new CommontTextAdapter(context,
				photoInfo.getCmtInfoList());
		viewHolder.comment_list.setAdapter(commontTextAdapter);

		viewHolder.comment_list
				.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						if (operationInterface != null) {
							operationInterface.commont(photoInfo, photoInfo
									.getCmtInfoList().get(position));
						}
					}
				});

		viewHolder.comment_list
				.setOnItemLongClickListener(new OnItemLongClickListener() {
					@Override
					public boolean onItemLongClick(AdapterView<?> parent,
							View view, int position, long id) {
						if (operationInterface != null) {
							operationInterface.commontDelete(photoInfo,
									photoInfo.getCmtInfoList().get(position));
						}
						return true;
					}
				});

		viewHolder.share_to.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (operationInterface != null) {
					operationInterface.shared(photoInfo);
				}
			}
		});
		viewHolder.report_to.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (operationInterface != null) {
					operationInterface.report(photoInfo);
				}
			}
		});
		final ViewHolder tempviewHolder = viewHolder;
		viewHolder.praise_to
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (operationInterface != null) {
							operationInterface.praise(photoInfo, buttonView,
									isChecked, tempviewHolder.praise_text,
									tempviewHolder.praise_image);
						}
					}
				});
		viewHolder.comment_to.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (operationInterface != null) {
					operationInterface.commont(photoInfo, null);
				}
			}
		});
		return convertView;
	}

	private void invisibilityDateInfo(ViewHolder viewHolder,
			Integer visibility, PhotoInfo photoInfo) {
		viewHolder.daynumber.setVisibility(visibility);
		viewHolder.mouthnumber.setVisibility(visibility);
		viewHolder.datetime.setVisibility(visibility);
		if (visibility == View.VISIBLE) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			try {
				calendar.setTime(format.parse(photoInfo.getDate()));
				viewHolder.daynumber.setText(""
						+ (calendar.get(Calendar.DATE) < 10 ? "0"
								+ calendar.get(Calendar.DATE) : calendar
								.get(Calendar.DATE)));
				viewHolder.mouthnumber.setText(""
						+ ((calendar.get(Calendar.MONTH) + 1) < 10 ? "0"
								+ (calendar.get(Calendar.MONTH) + 1)
								: (calendar.get(Calendar.MONTH) + 1)));
				viewHolder.datetime.setText(getWeek(calendar));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
	}

	private String getWeek(Calendar calenar) {
		String dayString = "星期一";
		switch (calenar.get(Calendar.DAY_OF_WEEK)) {
		case Calendar.MONDAY:
			dayString = "星期一";
			break;
		case Calendar.TUESDAY:
			dayString = "星期二";
			break;
		case Calendar.WEDNESDAY:
			dayString = "星期三";
			break;
		case Calendar.THURSDAY:
			dayString = "星期四";
			break;
		case Calendar.FRIDAY:
			dayString = "星期五";
			break;
		case Calendar.SATURDAY:
			dayString = "星期六";
			break;
		case Calendar.SUNDAY:
			dayString = "星期日";
			break;
		}
		return dayString;
	}
}
